package com.stu.design_patterns.singleton;

// 简单测试
public class Main {
  public static void main(String[] args) {
    test1();
    test2();
    test3();
    test4();
    test5();
  }

  // 测试饿汉式单例
  public static void test1() {
    Singleton01Hungry instance = Singleton01Hungry.getInstance();
    instance.say();
  }

  // 测试懒汉式单例
  public static void test2() {
    Singleton02Lazy instance = Singleton02Lazy.getInstance();
    instance.say();
  }

  // 测试双重检查锁式单例
  public static void test3() {
    Singleton03DoubleCheck instance = Singleton03DoubleCheck.getInstance();
    instance.say();
  }
  
  // 测试静态内部类式单例
  public static void test4() {
    Singleton04StaticInnerClass instance = Singleton04StaticInnerClass.getInstance();
    instance.say();
  }

  // 测试枚举式单例
  public static void test5() {
    Singleton05Enum instance = Singleton05Enum.INSTANCE;
    instance.say();
  }
}

// 测试单线程是否单例
class TestIsSingleton {
  public static void main(String[] args) {
    // test1();
    // test2();
    // test3();
    // test4();
    test5();
  }

  // 测试饿汉式单例
  public static void test1() {
    for(int i = 0; i < 100; i++) {
      System.out.println(Singleton01Hungry.getInstance());
    }
  }

  // 测试懒汉式单例
  public static void test2() {
    for(int i = 0; i < 100; i++) {
      System.out.println(Singleton02Lazy.getInstance());
    }
  }

  // 测试双重检查锁式单例
  public static void test3() {
    for(int i = 0; i < 100; i++) {
      System.out.println(Singleton03DoubleCheck.getInstance());
    }
  }
  
  // 测试静态内部类式单例
  public static void test4() {
    for(int i = 0; i < 100; i++) {
      System.out.println(Singleton04StaticInnerClass.getInstance());
    }
  }

  // 测试枚举式单例
  public static void test5() {
    for(int i = 0; i < 100; i++) {
      System.out.println(Singleton05Enum.INSTANCE.hashCode());
    }
  }
}

// 测试是否线程安全
class TestIsThreadSafe {
  public static void main(String[] args) {
    // test1();
    // test2();
    // test3();
    // test4();
    test5();
  }

  // 测试饿汉式单例
  public static void test1() {
    for(int i = 0; i < 100; i++) {
      new Thread(() -> {
        System.out.println(Singleton01Hungry.getInstance());
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }).start();
    }
  }

  // 测试懒汉式单例
  public static void test2() {
    for(int i = 0; i < 100; i++) {
      new Thread(() -> {
        System.out.println(Singleton02Lazy.getInstance());
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }).start();
    }
  }

  // 测试双重检查锁式单例
  public static void test3() {
    for(int i = 0; i < 100; i++) {
      new Thread(() -> {
        System.out.println(Singleton03DoubleCheck.getInstance());
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }).start();
    }
  }
  
  // 测试静态内部类式单例
  public static void test4() {
    for(int i = 0; i < 100; i++) {
      new Thread(() -> {
        System.out.println(Singleton04StaticInnerClass.getInstance());
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }).start();
    }
  }

  // 测试枚举式单例
  public static void test5() {
    for(int i = 0; i < 100; i++) {
      new Thread(() -> {
        System.out.println(Singleton05Enum.INSTANCE.hashCode());
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }).start();
    }
  }
}

